package com.test.test01.rabbitmq.receiver;

import com.rabbitmq.client.Channel;
import com.test.test01.config.rabbitmq.DelayRabbitConfig;
import com.test.test01.config.rabbitmq.RabbitMQConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Description: RabbitMQ 消费端
 * Create DateTime: 2020/10/26 9:31
 *
 * @author huangzi
 */
@Slf4j
@Component
public class RabbitMQConsumer {



    public static final String AUTO_CANCEL_REASON = "订单支付超时自动取消";

    /**
     * direct A
     *
     * @param message 消息
     */
    @RabbitListener(queues = RabbitMQConstant.RABBIT_DIRECT_QUEUEA)
    public void getDirectAMessage(Message<String> message, Channel channel) throws IOException {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("接收时间: "+df.format(new Date()));

        //获取传递过来的值
        String payload = message.getPayload();
        //执行订单支付超时逻辑
        log.info("获取directA队列传递过来的值,{}",payload);

        ack(message, channel);
    }


    /**
     * direct B
     */
    @RabbitListener(queues = RabbitMQConstant.RABBIT_DIRECT_QUEUEB)
    public void getDirectBMessage(Message<String> message, Channel channel) throws IOException {
        //获取传递过来的值
        String payload = message.getPayload();
        //执行订单支付超时逻辑
        log.info("获取directB队列传递过来的值,{}",payload);

        ack(message, channel);
    }

    /**
     * topic A
     */
    @RabbitListener(queues = RabbitMQConstant.RABBIT_TOPIC_A_QUEUE)
    public void getTopicAMessage(Message<String> message, Channel channel) throws IOException {
        //获取传递过来的值
        String payload = message.getPayload();
        //执行订单支付超时逻辑
        log.info("获取topicA队列传递过来的值,{}",payload);

        ack(message, channel);
    }

    /**
     * topic All
     */
    @RabbitListener(queues = RabbitMQConstant.RABBIT_TOPIC_B_QUEUE)
    public void getTopicAllMessage(Message<String> message, Channel channel) throws IOException {
        //获取传递过来的值
        String payload = message.getPayload();
        //执行订单支付超时逻辑
        log.info("获取topicAll队列传递过来的值,{}",payload);

        ack(message, channel);
    }

    /**
     * Fanout A
     */
    @RabbitListener(queues = RabbitMQConstant.RABBIT_FANOUT_QUEUEA)
    public void getFanoutAMessage(Message<String> message, Channel channel) throws IOException {
        //获取传递过来的值
        String payload = message.getPayload();
        //执行订单支付超时逻辑
        log.info("获取Fanout A队列传递过来的值,{}",payload);

        ack(message, channel);
    }

    /**
     * Fanout B
     */
    @RabbitListener(queues = RabbitMQConstant.RABBIT_FANOUT_QUEUEB)
    public void getFanoutBMessage(Message<String> message, Channel channel) throws IOException {
        //获取传递过来的值
        String payload = message.getPayload();
        //执行订单支付超时逻辑
        log.info("获取Fanout B队列传递过来的值,{}",payload);

        ack(message, channel);
    }

    /**
     * Fanout C
     */
    @RabbitListener(queues = RabbitMQConstant.RABBIT_FANOUT_QUEUEC)
    public void getFanoutCMessage(Message<String> message, Channel channel) throws IOException {
        //获取传递过来的值
        String payload = message.getPayload();
        //执行订单支付超时逻辑
        log.info("获取Fanout C队列传递过来的值,{}",payload);

        ack(message, channel);
    }

    /**
     * 延迟队列--交换机方式
     */
    @RabbitListener(queues = RabbitMQConstant.RABBIT_DELAY_EXPIRED_QUEUE)
    public void getDelayedMessage(Message<String> message, Channel channel) throws IOException {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("接收时间: "+df.format(new Date()));

        //获取传递过来的值
        String payload = message.getPayload();
        //执行订单支付超时逻辑
        log.info("延迟队列--交换机方式:->获取Delayed队列传递过来的值,{}",payload);

        ack(message, channel);
    }

    /**
     * 延迟队列--队列方式
     */
    @RabbitListener(queues = {DelayRabbitConfig.ORDER_QUEUE_NAME})
    public void orderDelayQueue(Message<String> message, Channel channel) throws IOException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("接收时间: "+df.format(new Date()));

        //获取传递过来的值
        String payload = message.getPayload();
        //执行订单支付超时逻辑
        log.info("延迟队列--队列方式:->获取Delayed队列传递过来的值,{}",payload);

        ack(message, channel);
    }


    /**
     * 手动确认消息
     *
     * @param message 消息
     * @param channel 队列
     */
    private void ack(Message message, Channel channel) throws IOException {
        Long deliveryTag = message.getHeaders().get(AmqpHeaders.DELIVERY_TAG, Long.class);
        channel.basicAck(deliveryTag, false);
    }
}
